/**
 * Created by lanyanhua on 2018/10/31 0031.
 */
NProgress.start();
window.onload = function () {
    NProgress.done();
};
layui.use(['element', 'table', 'form', 'jquery', 'laydate', 'list'], function () {
    var table = layui.table;
    var form = layui.form;
    var $ = layui.jquery;
    var list = layui.list;

    list.addField({
        field: {
            name: "jobGroupNo", label: "项目名", type: 'select', source: '/jobGroup/findJobGroupAll',
            valueName: 'groupNo', displayName: 'groupName', verify: ' '
        },
        select: ".jobGroupInline"
    });

    // 加载任务数据
    var showdatatable = table.render({
        elem: '#tableId22',
        id: "showdata",
        url: '/job/findJobAll',
        limit: 10,
        page: true,
        cols: [[
            {type: 'checkbox'}
            , {field: 'jobId', title: 'ID', sort: true, width: 60}
            , {field: 'jobName', title: '名称'}
            , {field: 'jobGroup', title: '项目名'}
            , {field: 'cronExpression', title: 'cron 表达式', width: 115}
            , {field: 'executeType', title: '执行策略', width: 60}
            , {field: 'jobStatus', title: '状态', templet: '#statusTpl', width: 81}
            , {field: 'emailServerId', title: '邮箱', hide: true}
            , {field: 'introduce', title: '介绍', templet: '#roleTpl'}
            , {
                field: 'createDate',
                title: '创建时间',
                templet: '<div>{{ Format(d.createDate,"yyyy-MM-dd hh:mm:ss")}}</div>'
            }
            , {
                field: 'updateDate',
                title: '修改时间',
                templet: '<div>{{ Format(d.updateDate,"yyyy-MM-dd hh:mm:ss")}}</div>'
            }
            , {title: '操作', templet: '#operationTpl', width: 100, align: 'center', fixed: 'right'}
        ]],
        done: function (res, curr, count) {
            $("#countNum").text(count);
        }
    });
    form.on('submit(search)', function () {
        showdatatable.reload({where: param()});
        return false;
    });
    // 弹框对象
    var listOpen = {
        elem: "#test6",
        fields: {
            jobId: {lable: 'ID', type: 'hidden'}
            , jobName: {label: "名称"}
            , jobGroup: {
                label: "项目名", type: 'select', source: '/jobGroup/findJobGroupAll',
                valueName: 'groupNo', displayName: 'groupName', readonly: "readonly"
            }
            , classPath: { label: '执行类', type: 'select', source: '/data/jobClass.json',
                valueName: 'class', displayName: 'name'   }
            , cronExpression: {label: "cron表达式"}
            , executeAddress: {label: "执行地址"}
            , executeType: {label: "执行策略"}
            , jobStatus: {
                label: "状态", type: 'radio',
                source: [{name: '启动', value: '0', checked: true}, {name: '暂停', value: '1'}]
            }
            , emailServerId: {
                label: "邮箱", type: 'select', source: '/email/findGroupEmail', ganged: 'jobGroup',
                valueName: 'emailServerId', displayName: 'emailName', verify: "false"
            }
            , emailType: {
                label: '邮箱发送策略', type: 'select', verify: "false",
                source: '/data/emailTypeData.json', valueName: 'id', displayName: 'value'
            }
            , introduce: {label: "介绍", type: "textarea"}
        },
        btns: ["submit", 'reset'],
        area: ['80%', '90%']
    };
    //console.log(listOpen);
    // 邮箱 联动
    form.on('select(jobGroupFilter)', function () {
        list.addField({
            field: {
                name: "emailServerId", label: "邮箱", type: 'select', source: '/email/findGroupEmail',
                valueName: 'emailServerId', displayName: 'emailName', verify: ' ', ganged: 'jobGroup'
            },
            select: ".jbf-input-emailServerId", form: form
        });

    });
    // 修改 任务状态
    form.on('switch(jobStatus)', function (obj) {
        var $this = this;
        layer.confirm("确定要修改当前状态吗？", {skin: 'layui-layer-lan', icon: 2, title: '提示', anim: 6}, function (index) {
            $.post('/job/updateStatus', {jobId: $this.value, jobStatus: obj.elem.checked ? 0 : 1}, function (data) {
                layer.close(index);
                if (data.success) {
                    layer.msg(data.message, {icon: 1, time: 1000});
                   // layui.table.reload('showdata', {page: {curr: 1}});
                } else {
                    layer.msg(data.message, {icon: 2, time: 5000});
                }
            })
        });
    });
    // 表格 操作 按钮
    table.on('tool(tableFilter)', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'edit') {
            //console.log(data);updateJob
            $.extend(true, listOpen, {
                fields: {
                    jobName: {readonly: "readonly"},
                    jobGroup: {type: "text", readonly: "readonly"}
                }
                , over: data
                , submit: function (res) {//表单提交事件
                    $.ajax({
                        url: '/job/updateJob',
                        type: 'post',
                        data: res.field,
                        dataType: 'json',
                        success: function (data) {
                            if (data.success) {
                                layer.msg(data.message, {icon: 1, time: 1000});
                                var index = layer.getFrameIndex(window.name);
                                layer.close(index); //再执行关闭
                                layui.table.reload('showdata', {page: {curr: 1}});
                            } else {
                                layer.msg(data.message, {icon: 2, time: 5000});
                            }
                        }
                    });
                    return false;
                }
            });
            list.open(listOpen);
        } else if (layEvent === 'del') {
            layer.confirm("确定要删除吗？", {skin: 'layui-layer-lan', icon: 2, title: '提示', anim: 6}, function () {
                $.ajax({
                    url: '/job/deleteJob'
                    , type: 'post'
                    , data: {
                        jobId: data.jobId,
                        jobName: data.jobName,
                        jobGroup: data.jobGroup
                    }, success: function (data) {
                        if (data.success) {
                            layer.msg(data.message, {icon: 1, time: 1000});
                            layui.table.reload('showdata', {page: {curr: 1}});
                        } else {
                            layer.msg(data.message, {icon: 2, time: 5000});
                        }
                    }
                })
            });
        }
    });
    // 新增 任务
    $("#addUser").click(function () {
        $.extend(true, listOpen, {
            fields: {
                jobName: {readonly: null},
                jobGroup: {type: "select", readonly: null}
            }
            , over: null
            , submit: function (res) {//表单提交事件
                $.ajax({
                    url: '/job/addJob',
                    type: 'post',
                    data: res.field,
                    dataType: 'json',
                    success: function (data) {
                        console.log("新增用户");
                        console.log(data);
                        if (data.success) {
                            layer.msg(data.message, {icon: 1, time: 1000});
                            // layer.close(index); //再执行关闭
                            layui.table.reload('showdata', {page: {curr: 1}});
                        } else {
                            layer.msg(data.message, {icon: 2, time: 5000});
                        }
                    }
                });
                return false;
            }
        });
        list.open(listOpen);
    });

    var $del = $("#batchEnabled");
    $del.off("click", "**");
    $del.on("click", function() {
    	var selectData = layui.table.checkStatus('tableId22').data;
        var rows = selectData.length;
        alert(rows);
    });
    function param() {
        return {
            jobName: $('[name=jobName]').val(),
            jobGroup: $('[name=jobGroupNo]').val()
        }
    }
});
